<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <script src="js/jquery.js"></script>
    <script src="layui/layui.js"></script>
    <link rel="stylesheet" href="./layui/css/layui.css">
</head>

<body>
    <label for="username">用户名</label>
    <input type="text" id="username" />
    <br>
    <label for="pwd">密码</label>
    <input type="password" id="pwd" />


    <br>
    <button onclick="login()">登录</button>
    <br>

    <textarea rows="5" cols="40" id="restext">
    </textarea>

    <br>
    <hr>
    <button onclick="place()">下订单</button>
    <br>
    <input type="text" id="sign" disabled>
    <br>
    <textarea rows="5" cols="40" id="">
    </textarea>

    


</body>


<script>
    // 登陆方法
    function login() {
        let user = {
            "account": $("#username").val(),
            "password": $("#pwd").val()
        }

        $.ajax({
            type: "POST",
            url: "http://localhost:10000/user/sign",
            data: JSON.stringify(user),
            success: function (res) {
                console.log("res:", res);
                $("#restext").val(res.data);
                localStorage.setItem("token", res.data);
            },
            contentType: "application/json;charset=UTF-8",
            // dataType:"json",
        })
    }

    // 下单方法
    function place() {
        let order = {
            // "userId": 1,
            "accept": "小礼",
            "telephone": "199831",
            "address": "88路无稽街3号",
            "item": [{
                "goodsId": 1,
                "nums": 5
            },
            {
                "goodsId": 2,
                "nums": 5
            }]
        }

        $.ajax({
            url: "http://localhost:10000/order/place",
            type: "POST",
            // beforeSend: function(xhr){
            //     xhr.setRequestHeader("Access-Token")
            // },
            headers: {
                'Access-Token': localStorage.getItem('token') == null ? " ": localStorage.getItem('token'),
            },
            data: JSON.stringify(order),
            success: function(res){
                console.log("place:",res);
                $("#sign").val(res.data);
            },
            contentType: "application/json;charset=UTF-8",
        })
    }

</script>

</html>